#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 505;
struct edge {
   int x, y; //坐标
   double v, a; //时间和特性
}e[N];

const int INF = 0x3f3f3f3f;
double dist[N][N];
int n;

void Init()
{
   for (int i = 1; i <= n; i++)
   {
       for (int j = 1; j <= n; j++)
       {
           if (i == j) dist[i][j] = 0;
           else dist[i][j] = INF;
       }
   }
}

void Floyd() 
{
   for (int k = 1; k <= n; k++) 
   {
       for (int i = 1; i <= n; i++) 
       {
           for (int j = 1; j <= n; j++) 
           {
               dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
           }
       }
   }
}

//在朋友帮助下达到最短时间
void Help(queue<edge> path, int C)
{
   Floyd();
   double res = dist[1][C];
   while (!path.empty()) {
       edge head = path.front(); 
       path.pop();
       double ans;
       if (fabs(head.a - 1) > 1e-8) 
       {
           ans = log(head.v * log(head.a)) / log(head.a);
       }
       ans = max(ans, dist[1][head.x]);
       res = min(res, ans + head.v * pow(head.a, -ans) + dist[head.y][C]);
   }
   printf("%.3f\n", res);
}

int main()
{
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   int C, R;
   while (cin >> C >> R) {

       if (C == 0 && R == 0) break;
       queue<edge> path;
       n = R + 1;

       Init();
       for (int i = 1; i < n; i++) {
           int x, y;
           double v, a;
           cin >> x >> y >> v >> a;
           dist[x][y] = dist[y][x] = v;
           e[i] = { x,y,v,a };
           path.push({ x,y,v,a });
           path.push({ y,x,v,a });
       }

       Help(path, C);
       
   }
   return 0;
}
